//
//  RootLaundryView.m
//  BaseFrame
//
//  Created by 吕金状 on 2017/8/1.
//  Copyright © 2017年 com.mxh. All rights reserved.
//

#import "RootLaundryView.h"
#import "RootLaundryCollectionViewCell.h"


@interface RootLaundryView ()<UICollectionViewDelegate , UICollectionViewDelegateFlowLayout , UICollectionViewDataSource>

Strong UICollectionView *collectionView;
Strong NSMutableArray *dataArray; /**<*/

@end

@implementation RootLaundryView

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self addSubview:self.collectionView];
    }
    return self;
}

- (UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
        [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
        flowLayout.minimumInteritemSpacing = 0;
        flowLayout.minimumLineSpacing = 0;
        flowLayout.headerReferenceSize = CGSizeMake(SCREEN_WIDTH, 34/WIDTH_5S_SCALE);
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH , SCREEN_WIDTH / 2 + 34 / WIDTH_5S_SCALE) collectionViewLayout:flowLayout];
        [_collectionView registerClass:[RootLaundryCollectionViewCell class] forCellWithReuseIdentifier:@"UICollectionView"];
        [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass([UICollectionReusableView class])];
        _collectionView.backgroundColor = getColor(@"ffffff");
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
    }
    return _collectionView;
}

#pragma mark - UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    RootLaundryCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"UICollectionView" forIndexPath:indexPath];
    cell.model = self.dataArray[indexPath.row];
    
    return cell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    UICollectionReusableView *view = nil;
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass([UICollectionReusableView class]) forIndexPath:indexPath];
        view.backgroundColor = [UIColor whiteColor];
        UILabel *titleLabel  = [[UILabel alloc]initWithFrame:CGRectMake(10 / WIDTH_5S_SCALE, 0, SCREEN_WIDTH, 34 / WIDTH_5S_SCALE)];
        titleLabel.font = DEF_FontSize_14;
        titleLabel.textColor = getColor(textColor);
        NSMutableAttributedString *attStr1 = [NSMutableAttributedString new];
        NSTextAttachment *seeAtt1 = [NSTextAttachment new];
        seeAtt1.bounds = CGRectMake(0, -2 / WIDTH_5S_SCALE, 3 / WIDTH_5S_SCALE, 12 / WIDTH_5S_SCALE);
        seeAtt1.image = [UIImage imageNamed:@"root_left"];
        [attStr1 appendAttributedString:[NSAttributedString attributedStringWithAttachment:seeAtt1]];
        [attStr1 appendAttributedString:[[NSAttributedString alloc] initWithString:@"  商城"]];
        titleLabel.attributedText = attStr1;
        
        UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 34 / WIDTH_5S_SCALE, SCREEN_WIDTH, 0.5)];
        lineView.backgroundColor = getColor(dividerColor);
        [view addSubview:lineView];
        [view addSubview:titleLabel];
    }
    return view;
}

- (NSAttributedString *)getAttributeName:(NSString *)name{
    NSMutableAttributedString *attStr = [NSMutableAttributedString new];
    NSAttributedString *nameAtt = [[NSAttributedString alloc]initWithString:@"|" attributes:@{NSForegroundColorAttributeName:getColor(mainColor)}];
    [attStr appendAttributedString:nameAtt];
    [attStr appendAttributedString:[[NSAttributedString alloc] initWithString:name]];
    return attStr;
}

#pragma mark -  UICollectionViewDelegate

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (_getRootLaundryClick) {
        _getRootLaundryClick(indexPath.row);
    }
}

#pragma mark - UICollectionViewDelegateFlowLayout

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(SCREEN_WIDTH / 2, 63 / WIDTH_5S_SCALE);
}


#pragma mark - data

- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray  = @[
                        @{@"title":@"我要洗衣",@"text":@"让您放心的服务",@"image":@"root_clothes"},
                        @{@"title":@"我要取衣",@"text":@"让您放心的服务",@"image":@"root_TakeTheClothes"},
                        @{@"title":@"会员充值",@"text":@"让您放心的服务",@"image":@"root_MemberPrepaidPhone"},
                        @{@"title":@"联系客户",@"text":@"让您放心的服务",@"image":@"root_ContachCeustomer"},
                        ].mutableCopy;
    }
    return _dataArray;
}

@end
